package com.fcx.workai.loginmodule.ui.login;/**
 * Created by hxh on 2017/8/4.
 */

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fcx.workai.loginmodule.R;
import com.fcx.workai.loginmodule.ui.smslogin.SMSLoginFragment;
import com.hxh.component.basicore.component.mvp.view.BaseFragment;
import com.hxh.component.business.arouter.MyAppProvider;
import com.hxh.component.business.common.UserInfoBean;
import com.hxh.component.business.ui.MyUtil;
import com.hxh.component.business.utils.Constant;


/**
 * 创建者：hxh
 * 时间：  2017/8/4
 * 描述：
 */
public class PwdLoginFragment extends BaseFragment<PwdLoginPresenter> implements PwdLoginContract.V {

    private EditText et_acount, et_pwd;
    private Button btn_login;
    private TextView tv_veritylogin,tv_jump_over;

    public static PwdLoginFragment newInstance() {
        Bundle args = new Bundle();
        PwdLoginFragment fragment = new PwdLoginFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static PwdLoginFragment newInstance(String pwd) {
        Bundle args = new Bundle();
        PwdLoginFragment fragment = new PwdLoginFragment();
        args.putString(DEFAULTKEY, pwd);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_pwdlogin;
    }

    @Override
    public void initData(Bundle saveInstanceState) {
        initView();
    }

    private void initView() {
        et_acount = findViewBy(R.id.et_acount);
        et_pwd = findViewBy(R.id.et_pwd);
        btn_login = findViewBy(R.id.btn_login);
        tv_veritylogin = findViewBy(R.id.tv_veritylogin);
        tv_jump_over = findViewBy(R.id.tv_jump_over);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!isEmpty(et_acount, "请输入手机号") && !isEmpty(et_pwd, "请输入密码")) {
                    if (MyUtil.isMobile(getText(et_acount))) {
//                        getP().login(getText(et_acount),getText(et_pwd), Constant.LOGINSCOPE_END_USER);
                        final String mTenantId ;
                        String baseUrl = com.hxh.component.business.BuildConfig.AUTH_ENDPOINT;
                        if ("https://api.workai.com.cn/".equals(baseUrl) || "http://120.55.17.174/".equals(baseUrl)){
                            mTenantId = Constant.TENANT_ID;
                        }else {
                            mTenantId = Constant.DEBUF_TENANT_ID;
                        }
                        getP().login(getText(et_acount), getText(et_pwd), Constant.LOGINSCOPE_END_USER + ",tenant:" + mTenantId);
                    } else {
                        toastShort("手机号码不正确！");
                    }
                }
            }
        });

        tv_veritylogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startWithPop(SMSLoginFragment.newInstance(et_acount.getText().toString()));
            }
        });

        tv_jump_over.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyAppProvider.startMainActivty("", "");
                getActivity().finish();
            }
        });

        String acount = getExtra_String(DEFAULTKEY);
        if (!isEmpty(acount)) {
            et_acount.setText("" + acount);
        }
    }

    @Override
    public com.fcx.workai.loginmodule.ui.login.PwdLoginPresenter newP() {
        return new PwdLoginPresenter();
    }

    @Override
    public void showLoginResule(UserInfoBean resule) {
        MyAppProvider.startMainActivty("", "");
        getActivity().finish();
    }
}
